package com.example.common.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer

abstract class BaseFragment : Fragment {
    constructor() : super()
    constructor(@LayoutRes layout: Int) : super(layout)

    private var mBinding: ViewDataBinding? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(getLayoutRes(), container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //将binding对象和view关联到一起
        mBinding = bindView(view,savedInstanceState)
        mBinding?.lifecycleOwner = viewLifecycleOwner
        initConfig()
        initData()
    }

    /*
   * open可以被子类重写 默认fun不能被重写和继承
   * 初始化配置
   * */
    open fun initConfig() { }

    //初始化数据
    open fun initData() { }

    abstract fun getLayoutRes(): Int

    abstract fun bindView(view: View, savedInstanceState: Bundle?): ViewDataBinding


    /*
   * 扩展liveData的observer函数
   * livedata回传回来的数据可能为null
   * */
    protected fun <T : Any> LiveData<T>.observeKt(block: (T) -> Unit) {
        this.observe(this@BaseFragment, Observer {
            block(it)
        })
    }
}